﻿using System;
using System.ComponentModel;
using System.Drawing;

namespace APP.HELPER.Utils.CustomizeToolbox
{
    public class VerticalString
    {
        private enum esaType
        {
            Pre,
            Post,
            Either
        }
        private double textSpread;
        public double TextSpread
        {
            get
            {
                return this.textSpread;
            }
            set
            {
                this.textSpread = value;
            }
        }
        [Description("VerticalString Constructor")]
        public VerticalString()
        {
            this.textSpread = 0.75;
        }
        [Description("Draw Method 1 - draw string in top left of rectangle.Calls Method 3")]
        public void Draw(Graphics g, string text, Font font, Brush brush, Rectangle stringRect)
        {
            this.Draw(g, text, font, brush, stringRect.X, stringRect.Y);
        }
        [Description("Draw Method 2 - draw string in rectangle according to Alignment and LineAlignmentCalls Method 3")]
        public void Draw(Graphics g, string text, Font font, Brush brush, Rectangle stringRect, StringFormat stringStrFmt)
        {
            int num;
            switch (stringStrFmt.Alignment)
            {
                case StringAlignment.Center:
                    num = stringRect.Width / 2 - (int)(font.Size / 2f) - 2;
                    break;
                case StringAlignment.Far:
                    num = stringRect.Width - (int)font.Size - 2;
                    break;
                default:
                    num = 0;
                    break;
            }
            double num2 = (double)this.Length(text, font);
            int num3;
            switch (stringStrFmt.LineAlignment)
            {
                case StringAlignment.Center:
                    num3 = stringRect.Height / 2 - (int)(num2 / 2.0);
                    break;
                case StringAlignment.Far:
                    num3 = stringRect.Height - (int)num2 - 2;
                    break;
                default:
                    num3 = 0;
                    break;
            }
            this.Draw(g, text, font, brush, stringRect.X + num, stringRect.Y + num3);
        }
        [Description("Draw Method 3 - draw string at coordinates x, y")]
        public void Draw(Graphics g, string text, Font font, Brush brush, int x, int y)
        {
            char[] array = text.ToCharArray();
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            Rectangle r = new Rectangle(x, y, (int)((double)font.Size * 1.5), font.Height);
            for (int i = 0; i < text.Length; i++)
            {
                r.Offset(0, this.ExtraSpaceAllowance(VerticalString.esaType.Pre, array[i], font));
                g.DrawString(array[i].ToString(), font, brush, r, stringFormat);
                r.Offset(0, (int)((double)font.Height * this.textSpread));
                r.Offset(0, this.ExtraSpaceAllowance(VerticalString.esaType.Post, array[i], font));
            }
        }
        [Description("Length Method - returns vertical length of string")]
        public int Length(string text, Font font)
        {
            char[] array = text.ToCharArray();
            int num = 0;
            for (int i = 0; i < text.Length; i++)
            {
                num += (int)((double)font.Height * this.textSpread);
                num += this.ExtraSpaceAllowance(VerticalString.esaType.Either, array[i], font);
            }
            return num;
        }
        private int ExtraSpaceAllowance(VerticalString.esaType type, char ch, Font font)
        {
            int result;
            if (this.textSpread >= 1.0)
            {
                result = 0;
            }
            else
            {
                int num = 0;
                if (type == VerticalString.esaType.Pre | type == VerticalString.esaType.Either)
                {
                    if (" bdfhijkltABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".IndexOf(ch) > 0)
                    {
                        num += (int)((double)font.Height * 0.2);
                    }
                }
                if (type == VerticalString.esaType.Post | type == VerticalString.esaType.Either)
                {
                    if (" gjpqyQ".IndexOf(ch) > 0)
                    {
                        num += (int)((double)font.Height * 0.2);
                    }
                }
                result = num;
            }
            return result;
        }
    }
}
